<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="./星星.svg" type="image/x-icon">
    <title>triangle</title>
</head>
<body>
    <canvas id="ca1"></canvas>
    <canvas id="ca2"></canvas>
</body>
<!-- 片元 -->
<script type="x-shader/x-fragment" id="fragment"> 
    void main() {
        gl_FragColor = vec4(1,1,0,1);
    }
</script>
<!-- 顶点 -->
<script type="x-shader/x-vertex" id="vertex">
    attribute vec4 a_Position ;
    void main() {
        gl_Position = a_Position;
        gl_PointSize = 10.0;
    }
</script>

<script type="module">
    import {initShaders} from './util.js';
    const canvas1 = document.querySelector('#ca1');
    canvas1.height = window.innerHeight - 30;
    canvas1.width = window.innerWidth/2 -10;
    const gl = canvas1.getContext('webgl');
    const vsText =  document.getElementById('vertex').textContent;
    const fsText =  document.getElementById('fragment').textContent;

    initShaders(gl, vsText, fsText);
    gl.clearColor(0.4, 0.5, 0.6, 0.7);
    gl.clear(gl.COLOR_BUFFER_BIT);

// 
const pointarr = [0,0, .5,0, 0,.5, .5,.5  ];
const points = new Float32Array(pointarr);

// 缓冲对象， 不会在一个宏任务结束后被清理
const vertexBuffer = gl.createBuffer();
// 绑定 target , buffer
gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer);
// 写入数据
gl.bufferData(gl.ARRAY_BUFFER, points, gl.STATIC_DRAW);

// 
 const a_Position = gl.getAttribLocation(gl.program, 'a_Position');
//                      index, size, type, normalized, stride, offset
 gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0)
//  赋能 批处理
gl.enableVertexAttribArray(a_Position);
// mode: number, first: number, count: number
// gl.drawArrays(gl.TRIANGLE_FAN,0,4);// 三角扇 第三条变为第一条边
// gl.drawArrays(gl.TRIANGLE_STRIP,0,4);// 三角带 第二条变为第三条边 可以写多个，
gl.drawArrays(gl.POINTS,0,1)
// gl.drawArrays(gl.LINES,0,4)//独立线段
gl.drawArrays(gl.LINE_LOOP,0,1)//封闭线
// gl.drawArrays(gl.LINE_STRIP,0,4)//不封闭线
// setTimeout(()=> {
//     gl.clearColor(0.4, 0.5, 0.6, 0.7);
//     gl.clear(gl.COLOR_BUFFER_BIT)
//     pointarr.push(.7,.6);
//     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(pointarr), gl.STATIC_DRAW);
//     gl.drawArrays(gl.POINTS,0,1);
//     gl.drawArrays(gl.LINE_STRIP,0,1);
// },1000)
function lianxian(count){
    
    const timer = setTimeout(()=> {
        count ++;
        if(count> 4) clearTimeout(timer);
    gl.drawArrays(gl.POINTS,0,count);
    gl.drawArrays(gl.LINE_LOOP,0,count);
    lianxian(count)
},1000)
}
lianxian(1)
</script>
</html>